#pragma once

#include "BaseInc.h"
#include "CPaint.h"
#include "CWindowBuffer.h"
#include "CRender.h"

class IWindow
{
public:
	virtual ~IWindow() {}
	virtual int_x HandleMSG(uint_32 uiMessage, uint_x uiParam, int_x iParam) = 0;
	virtual int_x SendMSG(uint_32 uiMessage, uint_x uiParam, int_x iParam) = 0;
	virtual bool PostMSG(uint_32 uiMessage, uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnDefault(uint_32 uiMessage, uint_x uiParam, int_x iParam) = 0;

	virtual int_x OnWmPaint(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmDestroy(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmShow(uint_x uiParam, int_x iParam) = 0;

	virtual int_x OnWmMouseMove(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmMouseEnter(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmMouseLeave(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmMouseDownL(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmMouseClickL(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmMouseUpL(uint_x uiParam, int_x iParam) = 0;

	virtual int_x OnWmMove(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmSize(uint_x uiParam, int_x iParam) = 0;

	virtual int_x OnWmEraseBack(uint_x uiParam, int_x iParam) = 0;

	virtual int_x OnWmSetText(uint_x uiParam, int_x iParam) = 0;
	virtual int_x OnWmActive(uint_x uiParam, int_x iParam) = 0;


	virtual void RenderWindow(const CRectInt32 & rect) = 0;
};

class CWindow : public CForm, public IWindow
{
public:
	CWindow();
	~CWindow();
	
public: // IForm
	void OnPaint(IPaint * pPaint, const CRectInt32 & rectClip) const;

	void ShowForm(FormShow show);
	void UpdateForm();

	void SetFormPosition(const CPointInt32 & ptForm);
	void SetFormSize(const CSizeInt32 & szForm);

	void SetFormCaption(const char_16 * szCaption);
	void SetFormActive(bool bActive);

public: // IControl
	void SetSize(const CSizeInt32 & size);
	void OnShow();
	bool SetCapture(bool bCapture);

	void Update();
	void Update(const CRectInt32 & rect);

public: // IWindow
	int_x HandleMSG(uint_32 uiMessage, uint_x uiParam, int_x iParam);
	int_x SendMSG(uint_32 uiMessage, uint_x uiParam, int_x iParam);
	bool PostMSG(uint_32 uiMessage, uint_x uiParam, int_x iParam);
	int_x OnDefault(uint_32 uiMessage, uint_x uiParam, int_x iParam);

	int_x OnWmMove(uint_x uiParam, int_x iParam);
	int_x OnWmSize(uint_x uiParam, int_x iParam);

	int_x OnWmShow(uint_x uiParam, int_x iParam);
	int_x OnWmPaint(uint_x uiParam, int_x iParam);
	int_x OnWmDestroy(uint_x uiParam, int_x iParam);
	int_x OnWmEraseBack(uint_x uiParam, int_x iParam);
	int_x OnWmMouseMove(uint_x uiParam, int_x iParam);
	int_x OnWmMouseEnter(uint_x uiParam, int_x iParam);
	int_x OnWmMouseLeave(uint_x uiParam, int_x iParam);
	int_x OnWmMouseDownL(uint_x uiParam, int_x iParam);
	int_x OnWmMouseClickL(uint_x uiParam, int_x iParam);
	int_x OnWmMouseUpL(uint_x uiParam, int_x iParam);
	int_x OnWmMouseDownR(uint_x uiParam, int_x iParam);
	int_x OnWmMouseClickR(uint_x uiParam, int_x iParam);
	int_x OnWmMouseUpR(uint_x uiParam, int_x iParam);
	int_x OnWmMouseDownM(uint_x uiParam, int_x iParam);
	int_x OnWmMouseClickM(uint_x uiParam, int_x iParam);
	int_x OnWmMouseUpM(uint_x uiParam, int_x iParam);

	int_x OnWmSetText(uint_x uiParam, int_x iParam);
	int_x OnWmActive(uint_x uiParam, int_x iParam);

	void RenderWindow(const CRectInt32 & rect);

public:
	void Create();
	void Create(int_32 iX, int_32 iY, int_32 iWidth, int_32 iHeight);
	HWND GetHandle() const;
	void SetHandle(HWND hWnd);

private:
	void ConfirmHandle() const;
protected:
	HWND m_hWnd;
	bool m_valid;
	CWindowBuffer m_buffer;
	bool m_bTracking;
	WNDPROC m_pfnDefWndProc;
};
